﻿/// File: Robotics4.NET\BodyMap\StickletBroker.cs
/// 
/// ------------------------------------------------------------
/// Copyright (c) 2004
///   Antonio Cisternino (cisterni.unipi.it),
///   Diego Colombo      (colombod.unipi.it),
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
#if WIN
using System.Collections.Generic;
using R4NRobletsList = System.Collections.Generic.List<Robotics4.NET.RobletBase>;
using R4nDomainList = System.Collections.Generic.List<System.AppDomain>;
using R4NConfigurationSettings = System.Configuration.ConfigurationSettings;
using R4NThread = System.Threading.Thread;
#elif CF
using R4NConfigurationSettings = OpenNETCF.Configuration.ConfigurationSettings;
using R4NThread = OpenNETCF.Threading.ThreadEx;
#endif


namespace Robotics4.NET
{
    public class StickletBroker : RobletBroker
    {
        /// <summary>
        /// 
        /// </summary>
        internal MessageManager ConfigDispatcher;
        /// <summary>
        /// 
        /// </summary>
        internal R4NThread ConfigListenerTH;

        StickletBroker()
        {
            UpdateSettings();
            RunningRoblets = new R4NRobletsList();
            Listener = null;
            ConfigListenerTH = null;
        }

        public new void UpdateSettings()
        {
            // Read ConfigurationListner settings
            NameValueCollection netconfig = R4NConfigurationSettings.GetConfig("Roblets_Workspace/Network") as NameValueCollection;

            int port = 6666;
            string host = "localhost";
            int localport = 6667;
            string brokernwif = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();
            if (netconfig["Port"] != null) port = int.Parse(netconfig["Port"] as string);
            if (netconfig["LocalPort"] != null) localport = int.Parse(netconfig["LocalPort"] as string);
            if (netconfig["BodyMapHost"] != null) host = netconfig["BodyMapHost"] as string;
            if (netconfig["BrokerInterface"] != null) brokernwif = netconfig["BrokerInterface"] as string;
            ConfigDispatcher = new MessageManager(localport, host, port, brokernwif);

            running = true;

            if (Listener != null) Listener.Abort();
            ConfigListenerTH = new R4NThread(new ThreadStart(ConfigListner));
            ConfigListenerTH.Name = "MessageListener";
            ConfigListenerTH.Start();
        }

        protected void ConfigListner()
        {
        }

        protected void SetupNetwork()
        {
            int port = 6666;
            string host = "localhost";
            int localport = 6667;
            string bodynwif = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();
//            if (netconfig["Port"] != null) port = int.Parse(netconfig["Port"] as string);
//            if (netconfig["LocalPort"] != null) localport = int.Parse(netconfig["LocalPort"] as string);
//            if (netconfig["BodyMapHost"] != null) host = netconfig["BodyMapHost"] as string;
            dispatcher = new MessageManager(localport, host, port, bodynwif);

            running = true;

            if (Listener != null) Listener.Abort();
            Listener = new R4NThread(new ThreadStart(MessageListener));
            Listener.Name = "MessageListener";
            Listener.Start();
        }

    }
}
